﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using IMCOM.Imagiality.ImageProcessing;
using IMCOM.Imagiality.ImageProcessing.Canvas2D;
using IMCOM.Imagiality.ImageProcessing.Contrast;
using IMCOM.Imagiality.ImageProcessing.Canvas2D.AutoContrast;
using IMCOM.Imagiality.Core;

namespace IMCOM.Imagiality.MainGUI
{
    public class Step1GUI : Panel
    {
        private const int ContrastMeasurerGUIHeight = 150;
        private const int RightPanelWidth = 200;

        private GenericImage image;
        private Canvas2DGUI canvas2DGUI;
        private ContrastMeasurerGUI contrastMeasurerGUI;
        private Panel panelRight;
        private Panel panelProjectionImage;
        private Label labelProjectionImage;
        private ComboBox comboBoxProjectionImage;
        private ContrastBalanceGUI contrastBalanceGUI;
        private CameraSettingsGUI cameraSettingsGUI;
        private Button buttonNext;
        private IList<Step1GUIListener> listeners;

        public GenericImage Image
        {
            get { return image; }
            set { image = value; setImage(value); }
        }

        public Step1GUI()
        {
            listeners = new List<Step1GUIListener>();

            Width = 640;
            Height = 640;

            canvas2DGUI = new Canvas2DGUI();
            Controls.Add(canvas2DGUI);

            contrastMeasurerGUI = new ContrastMeasurerGUI();
            Controls.Add(contrastMeasurerGUI);

            panelRight = new Panel();
            panelRight.Parent = this;

            panelProjectionImage = new Panel();
            panelRight.Controls.Add(panelProjectionImage);

            labelProjectionImage = new Label();
            panelProjectionImage.Controls.Add(labelProjectionImage);
            labelProjectionImage.Text = "Projection Image:";
            labelProjectionImage.TextAlign = ContentAlignment.MiddleLeft;

            comboBoxProjectionImage = new ComboBox();
            panelProjectionImage.Controls.Add(comboBoxProjectionImage);
            comboBoxProjectionImage.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxProjectionImage.Items.Add("Aim Image");
            comboBoxProjectionImage.Items.Add("White");
            comboBoxProjectionImage.Items.Add("Scan Setting Image");
            comboBoxProjectionImage.SelectedIndex = 0;

            contrastBalanceGUI = new ContrastBalanceGUI();
            panelRight.Controls.Add(contrastBalanceGUI);

            cameraSettingsGUI = new CameraSettingsGUI();
            panelRight.Controls.Add(cameraSettingsGUI);

            buttonNext = new Button();
            Controls.Add(buttonNext);
            buttonNext.Text = "Next";

            Resize += new EventHandler(GUIResized);

            ArrangeLayout();
        }

        private void setImage(GenericImage image)
        {
            IList<GenericImage> images = new List<GenericImage>();
            images.Add(image);
            
            CanvasImageWrapper imageWrapper = new CanvasImageWrapper(images);
            canvas2DGUI.ImageWrapper = imageWrapper;

            Canvas2DAutoContrastCalculator calculator = new Canvas2DAutoContrastCalculator(canvas2DGUI, contrastMeasurerGUI);
            calculator.Image = image;
            calculator.SampleCount = 1000;
            AutoContrastImageEventListener autoContrastListener = new AutoContrastImageEventListener(canvas2DGUI, calculator);
            canvas2DGUI.ImageEventListener = autoContrastListener;

            calculator.AddObserver(contrastMeasurerGUI);
        }

        public void AddListener(Step1GUIListener listener)
        {
            listeners.Add(listener);
            contrastBalanceGUI.AddListener(listener);
            cameraSettingsGUI.AddListener(listener);
        }

        private void GUIResized(object sender, System.EventArgs e)
        {
            ArrangeLayout();
        }

        private void ArrangeLayout()
        {
            canvas2DGUI.Location = new Point(5, 5);
            canvas2DGUI.Width = Width - 5 - RightPanelWidth - 5 - 5;
            canvas2DGUI.Height = Height - 5 - buttonNext.Height - 5 - ContrastMeasurerGUIHeight - 5;

            contrastMeasurerGUI.Location = new Point(5, canvas2DGUI.Bottom + 5);
            contrastMeasurerGUI.Width = Width - 5 - RightPanelWidth - 5 - 5;
            contrastMeasurerGUI.Height = ContrastMeasurerGUIHeight;

            panelRight.Location = new Point(Width - RightPanelWidth - 5, 0);
            panelRight.Width = RightPanelWidth;
            panelRight.Height = Height - 5 -buttonNext.Height - 5;

            panelProjectionImage.Location = new Point(0, 5);
            panelProjectionImage.Width = panelRight.Width;
            panelProjectionImage.Height = comboBoxProjectionImage.Height;

            labelProjectionImage.Location = new Point(0, 0);
            labelProjectionImage.Width = panelProjectionImage.Width / 2;
            labelProjectionImage.Height = comboBoxProjectionImage.Height;

            comboBoxProjectionImage.Location = new Point(panelProjectionImage.Width / 2, 0);
            comboBoxProjectionImage.Width = panelProjectionImage.Width / 2;

            contrastBalanceGUI.Width = panelRight.Width;
            contrastBalanceGUI.Location = new Point(0, panelProjectionImage.Bottom + 5);

            cameraSettingsGUI.Width = panelRight.Width;
            cameraSettingsGUI.Location = new Point(0, contrastBalanceGUI.Bottom + 5);
            
            buttonNext.Left = Width - buttonNext.Width - 5;
            buttonNext.Top = Height - buttonNext.Height - 5;
        }
    }
}
